<?php

// src/Application/Sonata/UserBundle/DataFixtures/ORM/LoadUserData.php
namespace Application\Sonata\UserBundle\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Application\Sonata\UserBundle\Entity\User;

class LoadUserData implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        require_once(__DIR__.'/../../../../../../external_scripts/DB.php');
        require_once(__DIR__.'/../../../../../../external_scripts/load_user.php');

        foreach( $dataList as $data )
        {
            $user = new User();
            $user->setId( $data["id"] );
            $user->setUsername( $data["username"] );
            $user->setFirstname( $data["first_name"] );
            $user->setLastname( $data["last_name"] );

            $user->setPassword( $data["password"] );
            $user->setEmail( $data["email_address"] );
            $user->setEnabled( (int) $data["is_active"] );
            $user->setLastLogin( new \DateTime($data["last_login"]) );
            $user->setGender( $data["type"] == "Mr" ? "m" : "f" );
            $user->setCreatedAt( new \DateTime($data["created_at"]) );
            $user->setUpdatedAt( new \DateTime($data["updated_at"]) );
            //$user->isSuperAdmin( true );

            $manager->persist($user);

            /* Pour forcer l'id */
            $metadata = $manager->getClassMetaData(get_class($user));
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);

            $manager->flush();
        }
    }

    /**
     * The order in which these fixtures will be loaded.
     */
    public function getOrder()
    {
        return 1;
    }
}

?>